<?php
namespace App\Tables;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Base
 * @package Illuminate\Database\Eloquent\Model
 */
class Base extends Model
{
    const CREATED_AT = '';
    const UPDATED_AT = '';
    public $timestamps = false;
    protected $dispatchesEvents = [  // 事件
        // ?? = App/asdf/asdf::class;
        //        'retrieved' => '??', // 恢复数据时调用的方法
        //        'creating'  => '??',
        //        'created'   => '??',
        //        'updating'  => '??',
        //        'updated'   => '??',
        //        'saving'    => '??',
        //        'saved'     => '??',
        //        'deleting'  => '??',
        //        'deleted'   => '??'
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setTableName();
    }

    /**
     * 设置表的名称 AbcDef => abc_def
     */
    private function setTableName()
    {
        $fullName = get_class($this);
        $fullArr  = explode('\\', $fullName);
        $nameArr = array_filter(str_split(lcfirst(array_pop($fullArr))));
        foreach($nameArr as $v) {
            if(preg_match('/[a-z]/', $v)){
                $this->table .= $v;
            } else {
                $this->table .= '_'.strtolower($v);
            }
        }
    }


}
